var myApp = angular.module('myApp', [ 'ngGrid', 'ui.select2' ]);
myApp.controller('constractorCtrl', function($scope, $http) {
	$scope.selectedRows = [];
	$scope.init = function() {	
			$scope.stList = [{"id":0, "lb":"在用"},{"id":1, "lb":"停用"}];
			httpPost($http, {"url":ctx + "/dict/getDictByCd", "params":{"cd":"shipmentMethod"}, "ifBlock":true}, function(data){
				$scope.list = data.dt;
			}, function(){
				toastError("初始化运输方式列表失败。");
			});
			httpPost($http, {"url":ctx + "/city/findListByFirst", "ifBlock":true}, function(data){
				$scope.cityList = data.dt;
			}, function(){
				toastError("初始化城市列表失败。");
			});		
	};
	$scope.init();
	
	$scope.pagingOptions = {
		pageSizes : [ 20, 50, 100 ],
		pageSize : 20,
		currentPage : 1,
		onQueryFlag : false
	};
	$scope.$watch('pagingOptions', function(newVal, oldVal) {
		if (newVal !== oldVal/* && newVal.currentPage !== oldVal.currentPage*/) {
			$scope.query();
		}
	}, true);
	$scope.gridOptions = {
		data : 'dt',
		totalServerItems : 'tt',
		
		columnDefs : [
		{
			field : "id",
			displayName : "ID", visible : false
		},
		{
			field : "cd",
			displayName : "代码",width : 120
		},
		{
			field : "name",
			displayName : "名称",width : 200
		},
		{
			field : "shortName",
			displayName : "简称",width : 150
		},
		{
			field : "cityName",
			displayName : "城市",width : 100
		},
		{
			field:"shipment_methodName",
			displayName:"运输方式",width : 100
		},
		{
			field : "linkMan",
			displayName : "联系人",width : 120
		},
		{
			field : "phone",
			displayName : "电话",width : 130
		},
		{
			field : "fax",
			displayName : "传真",width : 130
		},
		{
			field : "email",
			displayName : "邮件",width : 130
		},
		{
			field : "addr",
			displayName : "地址",width : 250
		},
		{
			field : "postCd",
			displayName : "邮编",width : 100
		},
		{
			field : "descr",
			displayName : "描述",width : 200
		},
		{
			field : "st",
			displayName : "状态",width : 100
		}
		],
		enablePaging : true,
		pagingOptions : $scope.pagingOptions,
		enableCellEdit : true,
		showSelectionCheckbox : true,
		selectedItems : $scope.selectedRows,
		plugins : [ /*new ngGridFlexibleHeightPlugin()*/ ],
		i18n : 'zh-cn', rowHeight : 21, footerRowHeight : 35,
		showFooter : true
	};

	$scope.query = function() {
		closeModal($("#queryFormModal"));
		var requestBody = {
			pageNb : $scope.pagingOptions.currentPage,
			pageSize : $scope.pagingOptions.pageSize,
			ffMap : $scope.queryForm
		};
		setTimeout(function() {
			httpPost($http, {"url":ctx + '/constractor/findListBy', "data" : requestBody, "ifBlock" : true}, function(data){
				$scope.tt = data.tt;
				$scope.dt = data.dt;
				$scope.gridOptions.$gridScope.toggleSelectAll(false);
				if(data.dt.length === 0){
					toastWarning("没有找到符合查询条件的记录。");
				};
			}, function(errMsg){
				toastError("查询失败。" + errMsg);
			});
		}, 100);
	};
	
	$scope.onQuery = function(){
		$scope.pagingOptions.onQueryFlag = !$scope.pagingOptions.onQueryFlag;
		$scope.pagingOptions.currentPage = 1;
	};

	$scope.refresh = function(){
		hideShadow();
		$scope.onQuery();	
	};
	
	$scope.onOpenExportFormModal = function(){	
		var params;
		if($scope.queryForm == undefined){
			params = "";
		}else{
			params = $scope.queryForm;
		}
		var str = jQuery.param(params);
		window.open(ctx  + '/constractor/export?' + str);
	};
	
	$scope.onClearSelectedRows = function() {
		$scope.gridOptions.$gridScope.toggleSelectAll(false);
	};
	
	$scope.onOpenQueryFormModal = function() {	
		hideShadow();
		openModal($("#queryFormModal"));
		if($scope.queryForm == undefined ){
			$scope.queryForm = {};
			$scope.queryForm.st = 0;
		}
	};
	
	$scope.onOpenEditFormModal = function(type, id) {
		if(type=="add"){			
			$scope.editForm = {};
			$scope.editForm.st = 0;
		}else if(type=="upd"){
			if($scope.selectedRows.length != 1) { toastWarning("请选择一笔记录进行编辑。"); return; }
			httpGet($http, {"url":ctx + '/constractor/id/' + $scope.selectedRows[0]['id'], "ifBlock" : true}, function(data){
				$scope.editForm = data.dt;
			}, function(errMsg){
				toastWarning("查询此笔记录失败。");
			});
		}
		hideShadow();
		openModal($("#editFormModal"));
	};
	
	$scope.onSaveEditForm = function(){
		closeModal($("#editFormModal"));
		httpPost($http, {"url":ctx + '/constractor/save', "data" : $scope.editForm, "ifBlock" : true}, function(data){
			toastSuccess("保存成功");
			$scope.query();
		}, function(errMsg){
			toastError("保存失败,您输入的信息有误，请检查正确之后再保存。" + errMsg);
		});
	};
	
	$scope.onOpenDelFormModal = function() {
		hideShadow();
		var selectedRowsLength = $scope.selectedRows.length;
		if(selectedRowsLength < 1) { toastWarning("请至少选择一笔记录进行删除。"); return; }
		dialogConfirm("是否确认删除选择的" + selectedRowsLength + "笔记录？", function(){
			var selectedRowsId = [];
			$.each($scope.selectedRows, function(idx, obj){
				selectedRowsId.push(obj.id);
			});
			
			httpPost($http, {"url":ctx + '/constractor/updStByIds', "params":{"ids" : selectedRowsId, "st" : 1}, "ifBlock":true}, function(data){
				toastSuccess("删除成功");
				$scope.query();
			}, function(errMsg){
				toastError("删除失败。" + errMsg);
			});
		});
	};
	
	//右键菜单功能
	$('#mainDataBlock').contextMenu('menu', {
	      //菜单项样式
	      itemStyle: {
	        fontFamily : 'verdana',
	        backgroundColor : '#F0F0F0',
	        border : 'none',
	        padding : '4px'
	      },
	      //菜单项鼠标放在上面样式
	      itemHoverStyle : {
	        border : '1px solid #fff'
	      },
	      onShowMenu : function(e, menu) {
		    	if($scope.selectedRows.length === 0){
		          $('#del, #edit', menu).remove();
		        }else if($scope.selectedRows.length > 1){
		          if($scope.selectedRows[0]['st'] === 0){
		        	  $('#edit', menu).remove(); 
		          }else{
		        	  $('#edit,#del', menu).remove();
		          }		  
		        }else{
		        	 if($scope.selectedRows[0]['st'] === 1){
		        		 $('#del', menu).remove();
		        	 }
		        }
		        return menu;
		      }
		});
});